<template>
  <div>
    <a-input-search
        v-model="positionText"
        :placeholder="placeholder"
        readOnly
        unselectable="on"
        @search="onSearchDepUser"
    >
      <a-button class="cu-btn-primary" slot="enterButton" :disabled="disabled"
      >选择岗位</a-button
      >
    </a-input-search>
    <t-select-Post-by-dep-modal
        ref="selectModal"
        :record="record"
        :modal-width="modalWidth"
        :multi="multiple"
        @ok="selectOK"
        :user-ids="userIds"
        @initComp="initComp"
    />
  </div>
</template>

<script>
  import TSelectPostByDepModal from "./modal/TSelectPostByDepModal";

  export default {
    name: "TSelectPositionByDep",
    components: { TSelectPostByDepModal },
    props: {
      modalWidth: {
        type: Number,
        default: 1250,
        required: false,
      },
      value: {
        type: String,
        required: false,
      },
      disabled: {
        type: Boolean,
        required: false,
        default: false,
      },
      multiple: {
        type: Boolean,
        default: true,
        required: false,
      },
      record: {
        type: Object,
      },
      placeholder: {
        type: String,
        defalut: "请选择用户",
      },
    },
    data() {
      return {
        userIds: "",
        positionText: "",
      };
    },
    mounted() {
      this.userIds = this.value;
    },
    watch: {
      value(val) {
        this.userIds = val;
      },
      record(val) {
        if (val) {
          this.userIds = val.options.defaultValue;
        }
      },
    },
    model: {
      prop: "value",
      event: "change",
    },
    methods: {
      initComp(positionText) {
        this.positionText = positionText;
      },
      onSearchDepUser() {
        this.$refs.selectModal.showModal();
      },
      selectOK(rows, idstr) {
        if (!rows) {
          this.positionText = "";
          this.userIds = "";
        } else {
          let temp = "";
          for (let item of rows) {
            temp += "," + item.positionText;
          }
          this.positionText = temp.substring(1);
          // console.log('positionText',this.positionText)
          this.userIds = idstr;
        }
        this.$emit("change", this.userIds);
        this.$emit("input", this.userIds);
      },
    },
  };
</script>

<style scoped></style>
